package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.domain.AppLookRecord;
import com.lebang.service.sys.AppLookRecordService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


/**
 * 浏览任务白名单
 *
 * @author hwq
 * @date 2020-03-30
 */
@Controller
@RequestMapping("/sys/applookrecord")
public class AppLookRecordController {
    @Autowired
    private AppLookRecordService baseService;

    @GetMapping("/list")
    public String list() {
        return "/applookrecord/list";
    }

    @GetMapping("/add")
    public String add(Integer id, Model model) {
        AppLookRecord vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppLookRecord();
        }
        model.addAttribute("item", vo);
        return "/applookrecord/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(AppLookRecord vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "删除成功");
    }
    @Login
    @PostMapping("/enableList")
    @ResponseBody
    public JsonData enableList(@RequestParam(name = "idlist", required = true) String idlist) {
        try {
            if (!TextUtil.isEmpty(idlist)) {

                List<String> list = Arrays.asList(idlist.split(","));
                int i = baseService.confirm(list);
                if (i > 0) {
                    return Callback.success("success", "操作成功", null);
                } else {
                    return Callback.fail("error", "操作失败", null);
                }
            } else {
                return Callback.fail("error", "参数异常", null);
            }
        } catch (Exception e) {
            return Callback.fail("error", "未知错误 ", e.getMessage());
        }
    }
}
